<template>
  <div>
    <QueryForm
      v-model="filters"
      :query-form-config="queryFormConfig"
      @handleQuery="getList(1)"
      @clear="clearFilters"
    />
    <div v-if="meta.indexOf('GymAdd') > -1" class="mb20">
      <el-button type="primary" @click="handleEdit()">新增</el-button>
    </div>
    <el-table
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      :header-row-class-name="'table-header'"
      class="doumee-element-table"
    >
      <el-table-column
        v-for="item in propList"
        :key="item.label"
        align="center"
        :label="item.label"
        :prop="item.prop"
        :min-width="item.minWidth"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        min-width="160"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button
              v-if="meta.indexOf('GymEdit') > -1"
              v-preventReClick
              type="text"
              @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              v-if="meta.indexOf('GymRenew') > -1"
              v-preventReClick
              type="text"
              @click="handleRenew(scope.row)"
              >续费</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <Paginations
      :total="total"
      :page="pagination.page"
      @current-change="getList"
      @SizeChange="handleSizeChange"
    />

    <el-dialog
      v-if="isShowEdit"
      :title="param.id ? '编辑场馆' : '新建场馆'"
      :visible.sync="isShowEdit"
      width="600px"
    >
      <el-form
        ref="form"
        :model="param"
        :rules="rules"
        label-width="120px"
        label-suffix="："
      >
        <el-form-item label="所属商户" prop="shopId">
          <el-select
            v-model="param.shopId"
            class="w400"
            placeholder="请选择所属商户"
          >
            <el-option
              v-for="item in shopList"
              :key="item.shopId"
              :label="item.shopName"
              :value="item.shopId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="场馆名称" prop="name">
          <el-input
            v-model="param.name"
            class="w400"
            placeholder="请输入场馆名称"
          />
        </el-form-item>
        <el-form-item label="生效时间" prop="startTime">
          <el-date-picker
            v-model="param.startTime"
            type="date"
            class="w400"
            value-format="yyyy-MM-dd"
            placeholder="日期选择"
          />
        </el-form-item>
        <el-form-item label="有效期" prop="year">
          <el-input
            v-model="param.year"
            onkeyup="value=value.replace(/[^\d^\.]/g,'')"
            onkeypress="return event.keyCode>=48&&event.keyCode<=57"
            style="width: 320px"
            placeholder="请输入有效期"
          />
          <span class="ml10">单位：年</span>
        </el-form-item>
        <el-form-item label="排序码">
          <el-input
            v-model="param.sortNum"
            onkeyup="value=value.replace(/[^\d^\.]/g,'')"
            onkeypress="return event.keyCode>=48&&event.keyCode<=57"
            class="w400"
            placeholder="请输入排序码"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowEdit = false">取 消</el-button>
        <el-button :loading="subLoading" type="primary" @click="onSubmit"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      v-if="isShowRenew"
      title="续费"
      :visible.sync="isShowRenew"
      width="600px"
    >
      <el-form
        ref="form"
        :model="param"
        :rules="rules"
        label-width="80px"
        label-suffix="："
      >
        <el-form-item label="续费" prop="year">
          <el-input
            v-model="param.year"
            onkeyup="value=value.replace(/[^\d^\.]/g,'')"
            onkeypress="return event.keyCode>=48&&event.keyCode<=57"
            style="width: 320px"
            placeholder="请输入"
          />
          <span class="ml10">单位：年</span>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="param.remark"
            type="textarea"
            :autosize="{ minRows: 5, maxRows: 5 }"
            class="w400"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isShowRenew = false">取 消</el-button>
        <el-button :loading="subLoading" type="primary" @click="onSubmitRenew"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { gymList, gymAdd, gymEdit, gymRenew, shopList } from '@/api'
import { queryVenueConfig, venuePropList, rules } from './config'
export default {
  components: {
  },
  data() {
    return {
      propList: venuePropList,
      queryFormConfig: queryVenueConfig,
      rules,
      listLoading: false,
      subLoading: false,
      meta: [],
      list: [],
      pagination: {
        page: 1,
        rows: 10
      },
      total: 0,
      filters: {},
      shopList: [],

      isShowEdit: false,
      param: {},
      isShowRenew: false
    }
  },
  created() {
    this.meta = this.$route.meta.buttons || []
    this.getList(1)
    this.getShops()
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated() {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  methods: {
    onSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const { param } = this
          this.subLoading = true
          const fn = param.id ? gymEdit : gymAdd
          fn({ param }).then(res => {
            if (res.errorCode !== '000000') return
            this.subLoading = false
            this.$message.success('保存成功')
            this.isShowEdit = false
            this.getList()
          }, () => {
            this.subLoading = false
          })
        }
      })
    },
    handleEdit(row) {
      this.param = {}
      this.isShowEdit = true
      if (row && row.id) {
        this.param = { ...row }
      }
    },
    handleRenew(row) {
      this.isShowRenew = true
      this.param = { ...row }
    },
    onSubmitRenew() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          const { param } = this
          this.subLoading = true
          gymRenew({ param }).then(res => {
            if (res.errorCode !== '000000') return
            this.subLoading = false
            this.$message.success('续费成功')
            this.isShowRenew = false
            this.getList()
          }, () => {
            this.subLoading = false
          })
        }
      })
    },
    handleSizeChange(value) {
      this.pagination.rows = value
      this.getList(1)
    },
    getList(page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      this.listLoading = true
      gymList({ pagination, param }).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          const temp = res.recordList || []
          this.list = temp.map(i => {
            i.shopId = i.shopId + ''
            return i
          })
          this.total = res.totalCount
        }
      }).catch(() => {
        this.listLoading = false
      })
    },
    getShops() {
      shopList({ param: { deployStatus: '1' }}).then(res => {
        this.shopList = res.recordList || []
      })
    },
    // 清空筛选项
    clearFilters() {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
